using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DiscGolfScore
{
	/// <summary>
	/// Summary description for HoleForm.
	/// </summary>
	public class HoleForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnNext;
		private System.Windows.Forms.Label lblDescription;
		private System.Windows.Forms.Panel pnlPlayers;
		private ArrayList players;
		private Course course;
		private System.Windows.Forms.Button btnPrevious;
		private int currentHoleNr = 1;
		private System.Windows.Forms.MainMenu mmuHoleMenu;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem mitAbort;
		private System.Windows.Forms.MenuItem miShowResults;
		private System.Windows.Forms.Form sourceForm;
		private System.Drawing.Font font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
		
		//reference to all the NumericUpDown controls on the screen
		private NumericUpDown[] nuds;
		private System.Windows.Forms.Label lblCurrentPlayer;

		//used for key operation
		private int currentPlayer = 0;
	
		public HoleForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			btnNext.BackColor = System.Drawing.Color.White;
			btnPrevious.BackColor = System.Drawing.Color.White;
			lblCurrentPlayer.BackColor = System.Drawing.Color.LimeGreen;
			lblCurrentPlayer.Font = font;
		}

		/// <summary>
		/// Add gui elements for each player and show form.
		/// </summary>
		/// <param name="players"></param>
		/// <param name="course"></param>
		public void Show( Form sourceForm, ArrayList players, Course course ) 
		{
			this.sourceForm = sourceForm;
			this.players = players;
			this.course = course;
			pnlPlayers.Controls.Clear();
			nuds = new NumericUpDown[players.Count];
			int count = 0;
			foreach (Player p in players) 
			{
				Hole currentHole = ((Hole)course.holes[0]);

				Label name = new Label();
				name.Text = p.FirstName+" "+p.LastName.Substring(0,1)+".";
				name.Left = 10;
				name.Top = 10 + 30*count;
				name.Font = font;
				pnlPlayers.Controls.Add(name);

				NumericUpDown nud = new NumericUpDown();
				nud.Minimum = 1;
				nud.Left = 168;
				nud.Width = 50;
				nud.Height = 25;
				nud.Top = 8 + 30*count;
				pnlPlayers.Controls.Add(nud);
				nuds[count] = nud;
				
				count++;
			}
			currentHoleNr = 1;
			ShowCurrentHole();
			Show();
		}

		/// <summary>
		/// Show hole data for each player
		/// </summary>
		/// <param name="hole"></param>
		private void ShowCurrentHole() 
		{
			currentPlayer = 0;
			lblCurrentPlayer.Top = pnlPlayers.Top + 10 + 30*currentPlayer;
			Hole currentHole = ((Hole)course.holes[currentHoleNr-1]);
			Text = course.Name+" hole "+currentHoleNr;
			lblDescription.Text = "Hole "+currentHoleNr+" (par "+currentHole.par+"): "+currentHole.description;
			for( int i=0; i<nuds.Length; i++ ) 
			{
				nuds[i].Value = ((Hole)((Player)players[i]).holesPlaying[currentHoleNr-1]).par;
			}
			this.Focus();	//make form focus so we can receive key events
		}
		/// <summary>
		/// Stores the hole information displayed in player data
		/// </summary>
		private void StoreCurrentHole() 
		{
			for( int i=0; i<nuds.Length; i++ ) 
			{
				((Hole)((Player)players[i]).holesPlaying[currentHoleNr-1]).par = (int)nuds[i].Value;
			}
		}

		private void addOneToCurrentPlayer() 
		{
			nuds[currentPlayer].Value++;
		}

		private void subtractOneFromCurrentPlayer() 
		{
			if( nuds[currentPlayer].Value > nuds[currentPlayer].Minimum ) 
			{
				nuds[currentPlayer].Value--;
			}
		}

		private void backToPreviousPlayer() 
		{
			//go to previous hole if this was first player
			if( currentPlayer <= 0 ) 
			{
				btnPrevious_Click(null, null);
			}
			else 
			{
				currentPlayer--;
				lblCurrentPlayer.Top = pnlPlayers.Top + 10 + 30*currentPlayer;
			}
		}

		private void proceedToNextPlayer() 
		{
			//proceed to next hole if this was last player
			if( currentPlayer >= players.Count-1 ) 
			{
				btnNext_Click( null, null);
			}
			else 
			{
				currentPlayer++;
				lblCurrentPlayer.Top = pnlPlayers.Top + 10 + 30*currentPlayer;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pnlPlayers = new System.Windows.Forms.Panel();
			this.btnNext = new System.Windows.Forms.Button();
			this.lblDescription = new System.Windows.Forms.Label();
			this.btnPrevious = new System.Windows.Forms.Button();
			this.mmuHoleMenu = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.miShowResults = new System.Windows.Forms.MenuItem();
			this.mitAbort = new System.Windows.Forms.MenuItem();
			this.lblCurrentPlayer = new System.Windows.Forms.Label();
			// 
			// pnlPlayers
			// 
			this.pnlPlayers.BackColor = System.Drawing.Color.LimeGreen;
			this.pnlPlayers.Location = new System.Drawing.Point(8, 40);
			this.pnlPlayers.Size = new System.Drawing.Size(224, 192);
			// 
			// btnNext
			// 
			this.btnNext.Location = new System.Drawing.Point(128, 240);
			this.btnNext.Size = new System.Drawing.Size(104, 20);
			this.btnNext.Text = "Next Hole";
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// lblDescription
			// 
			this.lblDescription.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
			this.lblDescription.Location = new System.Drawing.Point(8, 8);
			this.lblDescription.Size = new System.Drawing.Size(224, 32);
			this.lblDescription.Text = "Description";
			// 
			// btnPrevious
			// 
			this.btnPrevious.Location = new System.Drawing.Point(8, 240);
			this.btnPrevious.Size = new System.Drawing.Size(104, 20);
			this.btnPrevious.Text = "Previous Hole";
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			// 
			// mmuHoleMenu
			// 
			this.mmuHoleMenu.MenuItems.Add(this.menuItem1);
			// 
			// menuItem1
			// 
			this.menuItem1.MenuItems.Add(this.miShowResults);
			this.menuItem1.MenuItems.Add(this.mitAbort);
			this.menuItem1.Text = "Tools";
			// 
			// miShowResults
			// 
			this.miShowResults.Text = "Show results";
			this.miShowResults.Click += new System.EventHandler(this.miShowResults_Click);
			// 
			// mitAbort
			// 
			this.mitAbort.Text = "End game";
			this.mitAbort.Click += new System.EventHandler(this.mitAbort_Click);
			// 
			// lblCurrentPlayer
			// 
			this.lblCurrentPlayer.Location = new System.Drawing.Point(8, 48);
			this.lblCurrentPlayer.Size = new System.Drawing.Size(10, 20);
			this.lblCurrentPlayer.Text = ">";
			// 
			// HoleForm
			// 
			this.BackColor = System.Drawing.Color.LawnGreen;
			this.Controls.Add(this.btnPrevious);
			this.Controls.Add(this.btnNext);
			this.Controls.Add(this.lblDescription);
			this.Controls.Add(this.lblCurrentPlayer);
			this.Controls.Add(this.pnlPlayers);
			this.Menu = this.mmuHoleMenu;
			this.Text = "Hole 1";
			this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HoleForm_KeyUp);

		}
		#endregion

		private void btnNext_Click(object sender, System.EventArgs e)
		{
			StoreCurrentHole();
			//if next hole is last, show results button
			if( currentHoleNr == course.holes.Count-1 ) 
			{
				btnNext.Text = "Show results";
			}
				
			//show next hole
			if( currentHoleNr < course.holes.Count ) 
			{
				currentHoleNr++;
				ShowCurrentHole();
			}
			//or show results if this was last hole
			else 
			{
				ShowResults();
			}
		}

		/// <summary>
		/// Show the results dialog.
		/// </summary>
		private void ShowResults() 
		{
			ResultsForm results = new ResultsForm(players, course);
			results.ShowDialog();
		}

		private void btnPrevious_Click(object sender, System.EventArgs e)
		{
			StoreCurrentHole();
			if( currentHoleNr > 1 ) 
			{
				currentHoleNr--;
				ShowCurrentHole();
			}
			//if not at last hole any more, hide results button
			if( currentHoleNr == course.holes.Count-1 ) 
			{
				btnNext.Text = "Hext Hole";
			}
		}

		private void mitAbort_Click(object sender, System.EventArgs e)
		{
			sourceForm.Show();
			sourceForm.BringToFront();
			Hide();		
		}

		private void miShowResults_Click(object sender, System.EventArgs e)
		{
			ShowResults();
		}

		private void HoleForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode.Equals(Keys.Up) )
			{
				addOneToCurrentPlayer();
			}
			else if ( e.KeyCode.Equals(Keys.Down) )
			{
				subtractOneFromCurrentPlayer();
			}
			else if ( e.KeyCode.Equals(Keys.Left) )
			{
				backToPreviousPlayer();
			}
			else if ( e.KeyCode.Equals(Keys.Right) || e.KeyCode.Equals(Keys.Return) ) 
			{
				proceedToNextPlayer();
			}
		}


	}
}
